package com.woniu.user.mapper;

import com.woniu.common.entity.ApplyForTable;
import com.woniu.common.entity.Cancellation;
import com.woniu.common.entity.Dog;
import com.woniu.common.entity.User;
import com.woniu.user.entity.Perm;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {
    @Select("select * from user where account = #{account}")
    public User findUserByAccont(String account);

    //查出所有的rid
    @Select("select rid from user_role where uid = #{uid}")
    public List<Integer> findByRid(int uid);

    //查出所有的pid
    @Select("select pid from role_perms where rid = #{rid}")
    public List<Integer> findBypid(int rid);

    //通过pid查id
    @Select("select menuid from perms where pid = #{pid}")
    public List<Integer> findPermsid(int pid);

    //通过id查权限
    @Select("select menuid,menuname,pid,icon,url from perms where menuid = #{id}")
    public Perm findPerm(int id);

    //通过id查pid看看有没有子菜单
    @Select("select menuid from perms where pid = #{id}")
    public List<Integer> findAllPermsPid(int menuid);

    //修改信息
    @Update("update user set tel={tel},address=#{address}")
    boolean updateInfo(User user);

    //新增账号
    @Insert("insert into user(account,password) values (#{account},#{account})")
    boolean insertAccount(String account);

    //给角色添加普通用户权限
    @Insert("insert into user_role(uid,rid) values (#{uid},'1')")
    boolean addPersonalPerm(int uid);

    //给角色添加医院权限
    @Insert("insert into user_role(uid,rid) values (#{uid},'2')")
    boolean addHospitalPerm(int uid);

    //给角色添加警局权限
    @Insert("insert into user_role(uid,rid) values (#{uid},'3')")
    boolean addPolicePerm(int uid);
    //通过id查找角色
    @Select("select * from user where id = #{id}")
    User findUser(int id);

    //插入养宠申请信息
    @Insert("insert into apply_for_table(id,icd_image,person_image,ID_type,name," +
            "icd,sex,ethnic,domicile_address,tel,now_address,detailed_address,supporting_material," +
            "dog_name,dog_type,dog_sex,dog_weight,dog_heigh,dog_long,dog_positive_image,dog_side_image," +
            "person_info_audit,pet_infoaudit,u_id,apply_date) " +
            "values (default,#{icd_image},#{person_image},#{ID_type},#{name}," +
            "#{icd},#{sex},#{ethnic},#{domicile_address},#{tel},#{now_address},#{detailed_address},#{supporting_material}," +
            "#{dog_name},#{dog_type},#{dog_sex},#{dog_weight},#{dog_heigh},#{dog_long},#{dog_positive_image},#{dog_side_image}," +
            "#{person_info_audit},#{pet_infoaudit},#{u_id},#{apply_date})")
    boolean InsertApplyFor(ApplyForTable applyForTable);

    //查询养狗申请审批进度
    @Select("select * from apply_for_table where u_id = #{id}")
    List<ApplyForTable> raiseDog(int id);

    //查询该用户下的狗狗
    @Select("select * from dog where u_id = #{id}")
    List<Dog> findNormalPet(int id);

    //插入用户注销狗狗的申请
    /**
     * useGeneratedKeys="true":使用自动生成的主键
     * keyProperty:指定主键是(javaBean的)哪个属性,这里的javabean是account ,
     *             插入表中的主键值会通过调用account.setId写入到account的id属性中
     *
     *             注意：插入的主键值是通过javaBean的id获取，而不是通过方法返回值获取
     *
     * keyColumn:是数据库表中自增长主键的字段名为id
     * @param cancellation
     * @return void 注意这里返回空，插入的主键值不是通过return返回获取,
     * 插入表中的主键值会通过调用account.setId写入到account的id属性中
     */
    @Insert("insert into cancellation values" +
            "(default,#{cause},#{describe},#{prove_image},#{audit},#{failure_reason},#{u_id},#{pet_id},reviewer=#{reviewer}) ")
    @Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
    boolean cancelDog(Cancellation cancellation);

    //修改待注销狗狗申请状态的3(即紧急状态)
    @Update("update cancellation set audit='3' where id= #{id}")
    void updateCancelDog(int id);

    //查询所有待审核的注销狗狗申请
    @Select(("select * from cancellation where audit='0' or audit='3'"))
    List<Cancellation> selectCancelDog();

    //处理警局审批用户注销狗狗的申请
    @Update("update cancellation set " +
            "audit=#{audit},failure_reason=#{failure_reason},reviewer=#{reviewer}" +
            " where id= #{id}")
    boolean  manageCancelDog(Cancellation cancellation);

    //修改当前用户下这只狗的状态
    @Update("update dog set status='3' where id=#{id}")
    boolean changeDog(int id);

    //同意注销后修改状态
    @Update("update dog set status='2' where id=#{id}")
    boolean sureCancelDog(int id);

    //不同意注销后修改状态
    @Update("update dog set status='0' where id=#{id}")
    boolean noSureCancelDog(int id);
}
